define(['jquery', 'bootstrap', 'backend', 'table', 'form','qrcode'], function ($, undefined, Backend, Table, Form,qrcode) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'temple/index',
                    add_url: 'temple/add',
                    edit_url: 'temple/edit',
                    del_url: 'temple/del',
                    multi_url: 'temple/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                searchFormVisible:true,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'desc', title: __('Temple desc'),searchable:false},
                        {field: 'money', title: __('Solve money')},
                        {field: 'solve_count', title: __('Solve count'),searchable:false},
                        {field: 'qr_count', title: __('Qr count'),searchable:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('Note list'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-topic-list btn-dialog',
                                url:'note/index'
                            },{
                                name: 'detail',
                                text: __('Solve note list'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-topic-list btn-dialog',
                                url:'temple/user_note_list'
                            },{
                                name: 'detail',
                                text: __('Qr code'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-topic-list btn-dialog',
                                url:'temple/qr_code'
                            }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        user_note_list:function () {
            var temple_id = Config.temple_id;
            Table.api.init({
                extend: {
                    index_url: 'temple/user_note_list/ids/'+temple_id
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                searchFormVisible:true,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'user.nickname', title: __('Nickname')},
                        {field: 'temple_name', title: __('Temple name'),operate:false,searchable:false},
                        {field: 'note.name', title: __('Name')},

                        {field: 'note.desc', title: __('Desc')},
                        {field: 'is_solve', title: __('Is solve'), sortable:true,
                            searchList: {"0": __('No'), "1": __('Yes')},
                            formatter:function(value,row,index){
                                switch (value){
                                    case 0:
                                        return __('No');
                                    case 1:
                                        return __('Yes');
                                    default:
                                        return __('No');
                                }
                            } },
                        {field: 'solve_note_text', title: __('Solve note'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'content_text', title: __('Content'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'create_time', title: __('Create time'),
                            formatter: Table.api.formatter.datetime, operate: 'BETWEEN',
                            type: 'datetime', addclass: 'datetimepicker',
                            data: 'data-date-format="YYYY-MM-DD"'
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        qr_code : function () {
            var temple_id = Config.temple_id;
            var host = Config.host;
            $('#qr_code').qrcode(host+'/index/temple/index?temple_id='+temple_id);
        }
    };
    return Controller;
});